package cn.wizzer.app.web.modules.controllers.platform.cms;

import cn.wizzer.app.cms.modules.models.CmsBanner;
import cn.wizzer.app.cms.modules.services.CmsBannerService;
import cn.wizzer.app.user.modules.models.enums.CmsBannerEnum;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.app.web.commons.utils.PageUtil;
import cn.wizzer.framework.base.exceptions.BizException;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

@IocBean
@At("/platform/cms/banner")
public class CmsBannerController {
    private final static Log log = Logs.get();
    @Inject
    @Reference
    private CmsBannerService bannerService;

    @At("/")
    @Ok("beetl:/platform/cms/banner/index.html")
    @RequiresPermissions("cms.banner.list")
    public void index() {

    }

    /**
     * banner列表
     *
     * @param pageNumber    int
     * @param pageSize      int
     * @param pageOrderName String
     * @param pageOrderBy   String
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.banner.list")
    public Object data(@Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy, @Param("type") String type, @Param("language") String language) {
        Cnd cnd = Cnd.NEW();
        // Banner类型
        if (Strings.isNotBlank(type)) {
            cnd.and("type", "=", type);
        }
        // 语言
        if (Strings.isNotBlank(language)) {
            cnd.and("language", "=", language);
        }
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        } else {
            cnd.orderBy("opAt", "DESC");
        }
        return Result.success().addData(bannerService.listPage(pageNumber, pageSize, cnd));
    }

    /**
     * 新建Banner
     *
     * @param cmsBanner Banner
     * @return Object
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("cms.banner.add")
    @SLog(tag = "新建Banner")
    public Object add(@Param("..") CmsBanner cmsBanner) {
        // 调用校验方法
        Result result = bannerService.validateBanner(cmsBanner);
        // 校验成功则插入
        if (result.getCode() == 0) {
            for (String language : cmsBanner.getLanguage().split(",")) {
                if ("ABOUT".equals(cmsBanner.getType())) {
                    // ABOUT类型 各语言中唯一
                    if (bannerService.count(Cnd.where("language", "=", language).and("type", "=", "ABOUT")) > 0) {
                        throw new BizException("当前语言的ABOUT(关于)已存在");
                    }
                }
                cmsBanner.setLanguage(language);
                bannerService.insert(cmsBanner);
            }
            bannerService.removeCache();
        }
        return result;
    }

    /**
     * 删除Banner
     *
     * @param id String
     * @return Object
     */
    @At({"/delete/?", "/delete"})
    @Ok("json")
    @RequiresPermissions("cms.banner.delete")
    public Object delete(String id, @Param("ids") String[] ids) {
        if (null != ids && ids.length > 0) {
            bannerService.delete(ids);
        } else {
            bannerService.delete(id);
        }
        bannerService.removeCache();
        return Result.success();
    }

    /**
     * 启用Banner
     *
     * @param id String
     * @return Object
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("cms.banner.edit")
    @SLog(tag = "启用Banner")
    public Object enable(String id) {
        bannerService.update(Chain.make("disabled", false), Cnd.where("id", "=", id));
        bannerService.removeCache();
        return Result.success();
    }

    /**
     * 禁用Banner
     *
     * @param id String
     * @return Object
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("cms.banner.edit")
    @SLog(tag = "禁用Banner")
    public Object disable(String id) {
        bannerService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
        bannerService.removeCache();
        return Result.success();
    }


    /**
     * 查询Banner
     *
     * @param id String
     * @return Object
     */
    @At("/edit/?")
    @Ok("json:full")
    public Object edit(String id) {
        return Result.success().addData(bannerService.fetch(id));
    }


    /**
     * 修改Banner
     *
     * @param cmsBanner Banner
     * @return Object
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("cms.banner.edit")
    @SLog(tag = "修改Banner")
    public Object editDo(@Param("..") CmsBanner cmsBanner) {
        Result result = bannerService.validateBanner(cmsBanner);
        if (result.getCode() == 0) {
            if ("ABOUT".equals(cmsBanner.getType())) {
                // ABOUT类型 各语言中唯一
                if (bannerService.count(Cnd.where("language", "=", cmsBanner.getLanguage()).and("type", "=", "ABOUT").and("id","!=",cmsBanner.getId())) > 0) {
                    throw new BizException("当前语言的ABOUT(关于)已存在");
                }
            }
            bannerService.update(cmsBanner);
            bannerService.removeCache();
        }
        return result;
    }

    /**
     * BANNER类型枚举
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("cms.banner.list")
    public Object status() {
        return Result.success(CmsBannerEnum.toList());
    }

}




